package com.gzhryc.shared_device.oem.code.business.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.business.dao.PlaceRecommendationDao;
import com.gzhryc.shared_device.oem.code.business.dao.db.PlaceRecommendation;
import com.gzhryc.shared_device.oem.code.business.services.dto.PlaceRecommendationSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class PlaceRecommendationService {

    static Logger log = Logger.getLogger(PlaceRecommendationService.class);

    public static PlaceRecommendationService self() {
        return MultiDBTools.getService(PlaceRecommendationService.class, true);
    }

    public static PlaceRecommendationService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PlaceRecommendationService.class, true);
    }

    PlaceRecommendationDao dao;

    public PlaceRecommendationService(String jdbcKey) {
        this.dao = new PlaceRecommendationDao(jdbcKey);
    }

    public List<PlaceRecommendation> findBySearch(PlaceRecommendationSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(PlaceRecommendationSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public PlaceRecommendation getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public PlaceRecommendation getByPhone(String phone){
        if(StringTools.isNotBlank(phone)){
            Conditions conditions = new Conditions(PlaceRecommendation.class);
            conditions.and().is("phone",phone);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(PlaceRecommendation placeRecommendation) throws LogicException {
        PlaceRecommendation temp = getByPhone(placeRecommendation.getPhone());
        if(temp != null){
            return true;
        }
        placeRecommendation.setCreateDate(new Date());
        try {
            return dao.insert(placeRecommendation) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
